<?php

namespace frontend\components;

use Yii;
use frontend\models;


class PreBehavior extends \yii\base\ActionFilter
{
    public function beforeAction ($action)
    {
        
        //加载配置文件 
        $this->loadConfig();
        
        //加载主题
        $this->loadTheme();
        //定义主题路径常量 
        define("THEME_ASSETS",Yii::$app->params['viewPath']."/".Yii::$app->params['theme']."/assets");
        
        
        
        return true;
    }
    
    
    //加载主题  
    private function loadTheme(){
        
        $config['value'] = \Yii::$app->params['THEME'] ?\Yii::$app->params['THEME'] :"default";
        //修改内容
        \Yii::$app->params['theme'] = $config['value'];
        $components = \Yii::$app->components;
        
        $components['view']['theme']['pathMap']['@app/views'] = "@app/views/".$config['value'];
        
        \Yii::$app->setComponents($components);
        
 
    }
    
    /**
     *  加载后台测试 的配置文件  
     *  加载后的配置 将会与 yii::$app->params 配置文件合并
     * **/
   
    private function loadConfig(){
        
        $configs  = \Yii::$app->db->createCommand("select name,value from {{config}}")->queryAll();
        $configsFixed = array();
        foreach($configs as $config){
            $configsFixed[$config['name']] = $config['value'];
        }
        
        \Yii::$app->params = array_merge($configsFixed,  \Yii::$app->params);
    }
    
    
    
  

    
}